package com.example.shixi.service;

import com.example.shixi.domain.SysOperLog;
import com.example.shixi.domain.SysOperLogExample;
import com.example.shixi.mapper.SysOperLogMapper;
import com.example.shixi.pojo.UserPage;
import com.example.shixi.util.JSONResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class LogService {
    @Resource
    private SysOperLogMapper sysOperLogMapper;
    public JSONResult query(SysOperLog realName, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        SysOperLogExample sysOperLogExample = new SysOperLogExample();
        SysOperLogExample.Criteria criteria = sysOperLogExample.createCriteria();
        //SysOperLogExample sysOperLogExampleAll = new SysOperLogExample();
        UserPage<SysOperLog> userPage= new UserPage<>();

        //List<SysOperLog> sysOperLogsAll = sysOperLogMapper.selectByExample(sysOperLogExampleAll);
        if(!ObjectUtils.isEmpty(realName)){
            criteria.andRealNameLike("%"+realName.getRealName()+"%");
        }

        List<SysOperLog> sysOperLogs = sysOperLogMapper.selectByExample(sysOperLogExample);
        PageInfo<SysOperLog> pageInfo= new PageInfo<>(sysOperLogs);
        userPage.setList(sysOperLogs);
        userPage.setTotal(pageInfo.getTotal());
        userPage.setPageNum(pageNum);
        userPage.setPageSize(pageSize);
        return JSONResult.success(userPage);

    }
    public JSONResult del(Long id) {

        try {
            sysOperLogMapper.deleteByPrimaryKey(id);
            return JSONResult.success("删除成功了捏");
        }catch (Exception e){
            e.printStackTrace();
            return JSONResult.error("别问，问就是不知道");
        }

    }
    public void add(String updateName,String content){
        Date date = new Date();
        SysOperLog sysOperLog = new SysOperLog();
        sysOperLog.setContent(content);
        sysOperLog.setRealName(updateName);
        sysOperLog.setCreateTime(date);
        sysOperLogMapper.insertSelective(sysOperLog);
    }
}
